package cn.com.chec.en.controller.admin;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateFormatUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import cn.com.chec.en.dao.ChannelInfoDao;
import cn.com.chec.en.dao.ContentInfoDao;
import cn.com.chec.en.dao.LanguageInfoDao;
import cn.com.chec.en.dao.LogInfoDao;
import cn.com.chec.en.dao.NewsInfoDao;
import cn.com.chec.en.dao.TagInfoDao;
import cn.com.chec.en.dao.UserInfoDao;
import cn.com.chec.en.dao.WebsiteInfoDao;
import cn.com.chec.en.model.domain.ChannelInfo;
import cn.com.chec.en.model.domain.ContentInfo;
import cn.com.chec.en.model.domain.LanguageInfo;
import cn.com.chec.en.model.domain.NewsChannelRel;
import cn.com.chec.en.model.domain.NewsInfo;
import cn.com.chec.en.model.domain.QueryNewsObject;
import cn.com.chec.en.model.domain.TagInfo;
import cn.com.chec.en.model.domain.UserInfo;
import cn.com.chec.en.model.domain.WebsiteInfo;
import cn.com.chec.en.model.dto.NewsDTO;
import cn.com.chec.en.util.PageBean;
import cn.com.chec.en.util.RegexUtils;
import cn.com.chec.en.util.StringTools;
import cn.com.chec.en.util.WebUtils;

@Controller
@RequestMapping({ "/admin/news" })
public class NewsInfoAdminController {
	protected static final Logger logger = Logger
			.getLogger(NewsInfoAdminController.class);

	@Resource
	private UserInfoDao userInfoDao;

	@Resource
	private LogInfoDao logInfoDao;

	@Resource
	private LanguageInfoDao languageInfoDao;

	@Resource
	private ChannelInfoDao channelInfoDao;

	@Resource
	private TagInfoDao tagInfoDao;

	@Resource
	private NewsInfoDao newsInfoDao;

	@Resource
	private WebsiteInfoDao websiteInfoDao;

	@Resource
	private ContentInfoDao contentInfoDao;

	@Resource
	private Map<String, Object> environment;

	@SuppressWarnings("unchecked")
	@RequestMapping({ "/showInsert" })
	public ModelAndView showInsert(HttpServletRequest request,
			HttpSession session, ModelMap map) throws Exception {
		ModelAndView mav = new ModelAndView("admin/newsInfo/newsInsert");
		List<LanguageInfo> languageInfoList = this.languageInfoDao.getAll();
		int languageId = StringUtils.isNotBlank(request
				.getParameter("languageId")) ? Integer.valueOf(
				request.getParameter("languageId")).intValue() : 1;
		List<ChannelInfo> channelInfoList = this.channelInfoDao
				.getSubListByParentIdLanguageId(1, 0, 0, 2147483647);
		List<UserInfo> userInfoList = this.userInfoDao.getUserListByRoleId(-1, 0,
				2147483647);
		map.put("languageInfoList", languageInfoList);
		map.put("channelInfoList", channelInfoList);
		map.put("userInfoList", userInfoList);
		map.put("act", "insert");
		map.put("userRightsSession",
				(List<String>) session.getAttribute("systemUserRigthList"));
		map.put("userInfoSession",
				(UserInfo) session.getAttribute("systemUserInfo"));
		map.put("languageId", Integer.valueOf(languageId));
		map.put("currentTime",
				new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
		map.putAll(this.environment);
		mav.addAllObjects(map);
		return mav;
	}

	@RequestMapping({ "/insert" })
	public ModelAndView insert(HttpServletRequest request, HttpSession session,
			ModelMap map) throws Exception {
		ModelAndView mav = new ModelAndView("admin/common/message");
		StringBuilder message = new StringBuilder();
		int userId = ((UserInfo) session.getAttribute("systemUserInfo"))
				.getId();

		int languageId = StringUtils.isNotBlank(request
				.getParameter("languageId")) ? Integer.parseInt(request
				.getParameter("languageId")) : 1;
		String channelIds = request.getParameter("channelIds");
		String title = request.getParameter("title");
		String tagWordsId = request.getParameter("tagWordsId");

		String from = request.getParameter("from");
		String publishTime = request.getParameter("publishTime");
		int isFouceImage = StringUtils.isNotBlank(request
				.getParameter("isFouceImage")) ? Integer.parseInt(request
				.getParameter("isFouceImage")) : 0;
		String keywords = request.getParameter("keywords");
		String newsMeta = request.getParameter("newsMeta");

		int isShowInIndex = StringUtils.isNotBlank(request
				.getParameter("isShowInIndex")) ? Integer.parseInt(request
				.getParameter("isShowInIndex")) : 0;
		int isShowInList = StringUtils.isNotBlank(request
				.getParameter("isShowInList")) ? Integer.parseInt(request
				.getParameter("isShowInList")) : 1;
		int isTop = StringUtils.isNotBlank(request.getParameter("isTop")) ? Integer
				.parseInt(request.getParameter("isTop")) : 0;
		int isReview = StringUtils.isNotBlank(request.getParameter("isReview")) ? Integer
				.parseInt(request.getParameter("isReview")) : 0;

		int textType = StringUtils.isNotBlank(request.getParameter("textType")) ? Integer
				.parseInt(request.getParameter("textType")) : 1;
		String content = StringUtils
				.isNotBlank(request.getParameter("content")) ? request
				.getParameter("content") : "";
		String simpleText = StringUtils.isNotBlank(request
				.getParameter("simpleText")) ? request
				.getParameter("simpleText") : "";

		if (textType == 2) {
			content = simpleText;
		}
		String mainImage = "";
		String srcRegex = "src\\s*=\\s*[\"|'](.+?[jpg|gif|png|bmp|ico])[\"|']";
		List<String> allImages = RegexUtils.getStringGoup1(content, srcRegex);
		//图片倒序排列
		List<String> reverseOrder = new ArrayList<String>();
		for(int i = allImages.size() -1 ; i > -1 ; i --){
			reverseOrder.add(allImages.get(i));
		}
//		Collections.sort(allImages, Collections.reverseOrder());
		String appPath = request.getContextPath();
		String appURL = request.getScheme() + "://" + request.getServerName()
				+ ":" + request.getServerPort();
		String contentResult = content;
		for (String image : reverseOrder) {
			if (!image.toLowerCase().startsWith("http")) {
				if ((StringUtils.isNotBlank(appPath))
						&& (appPath.length() == 1)) {
					mainImage = image.substring(1);
					contentResult = content.replace(image, appURL + image);
				} else {
					mainImage = image.substring(appPath.length() + 1);
					contentResult = content.replace(image, appURL + image);
				}
			} else {
				mainImage = image;
			}
		}
		NewsInfo newsInfo = new NewsInfo();
		newsInfo.setLanguageId(languageId);
		newsInfo.setTitle(title);
		newsInfo.setTag(tagWordsId);
		UserInfo userInfoSelect = this.userInfoDao.getUser(userId);
		newsInfo.setAuthor(userInfoSelect.getRealName());
		newsInfo.setUserId(userId);
		newsInfo.setFrom(from);
		newsInfo.setPublishTime(publishTime);
		newsInfo.setIsFouceImage(isFouceImage);
		newsInfo.setMainImage(mainImage);
		newsInfo.setKeywords(keywords);
		newsInfo.setNewsMeta(newsMeta);
		newsInfo.setTextType(textType);
		newsInfo.setContent(contentResult);
		newsInfo.setChannelIds(channelIds);

		NewsChannelRel rel = new NewsChannelRel();
		rel.setIsShowInIndex(isShowInIndex);
		rel.setIsShowInList(isShowInList);
		rel.setIsReview(isReview);
		rel.setIsTop(isTop);

		NewsDTO newsDTO = new NewsDTO();
		newsDTO.setNewsInfo(newsInfo);
		newsDTO.setRel(rel);

		Map<String, List<String>> resultMap = this.newsInfoDao.insert(newsDTO);
		if (resultMap.size() > 0) {
			message.append("操作成功!<br/>");

			List<String> messageIn = resultMap.get("1");
			if ((messageIn != null) && (messageIn.size() > 0)) {
				message.append("插入指定栏目:<br/>");
			}
			for (String s : messageIn) {
				message.append(s).append("<br/>");

				this.logInfoDao.log(userId, 1, this.channelInfoDao
						.getChannelByChannelName(s).getChannelId(), newsInfo
						.getTitle());
			}

			List<String> messageUp = resultMap.get("2");
			if ((messageUp != null) && (messageUp.size() > 0)) {
				message.append("更新指定栏目:<br/>");
			}
			for (String s : messageUp) {
				message.append(s).append("<br/>");

				this.logInfoDao.log(userId, 3, this.channelInfoDao
						.getChannelByChannelName(s).getChannelId(), newsInfo
						.getTitle());
			}

			String messageId = request.getParameter("messageId");
			StringUtils.isNotBlank(messageId);
		} else {
			message.append("操作失败!");
		}
		map.put("message", message.toString());
		mav.addAllObjects(map);
		return mav;
	}

	@SuppressWarnings("unchecked")
	@RequestMapping({ "/showUpdate" })
	public ModelAndView showUpdate(HttpServletRequest request,
			HttpSession session, ModelMap map) throws Exception {
		ModelAndView mav = new ModelAndView("admin/newsInfo/newsInfoEdit");
		int newsId = StringUtils.isNotBlank(request.getParameter("newsId")) ? Integer
				.parseInt(request.getParameter("newsId")) : 0;
		int relId = StringUtils.isNotBlank(request.getParameter("relId")) ? Integer
				.parseInt(request.getParameter("relId")) : 0;
		int languageId = StringUtils.isNotBlank(request
				.getParameter("languageId")) ? Integer.parseInt(request
				.getParameter("languageId")) : 1;
		NewsDTO newsDTO = this.newsInfoDao.getNewsDTOInfoById(newsId, relId);

		String channelIds = "";
		String channelNames = "";
		String chIds = "";
		String chNames = "";
		for (ChannelInfo clInfo : newsDTO.getChannelList()) {
			chIds = chIds + "," + clInfo.getChannelId();
			chNames = chNames + "," + clInfo.getChannelName();
		}
		chIds = (StringUtils.isNotBlank(chIds)) && (chIds.length() > 0) ? chIds
				.substring(1) : "";
		chNames = (StringUtils.isNotBlank(chNames)) && (chNames.length() > 0) ? chNames
				.substring(1) : "";
		channelIds = chIds;
		channelNames = chNames;

		String tagWordsId = "";
		String tagWords = "";
		tagWordsId = newsDTO.getNewsInfo().getTag();
		if (StringUtils.isNotBlank(tagWordsId)) {
			String[] tagIds = tagWordsId.split(",");
			for (String sss : tagIds) {
				if ((StringUtils.isBlank(sss)) || (!sss.matches("\\d+"))) {
					continue;
				}
				TagInfo ti = this.tagInfoDao.getTagById(Integer.valueOf(sss)
						.intValue());
				tagWords = tagWords + (ti != null ? ti.getTag() + "," : "");
			}
		}
		List<LanguageInfo> languageInfoList = this.languageInfoDao.getAll();
		List<ChannelInfo> channelInfoList = this.channelInfoDao.getALL();
		map.put("channelIds", channelIds);
		map.put("channelNames", channelNames);
		map.put("tagWordsId", tagWordsId);
		map.put("tagWords", tagWords);
		map.put("newsDTO", newsDTO);
		map.put("languageInfoList", languageInfoList);
		map.put("channelInfoList", channelInfoList);
		map.put("userRightsSession",
				(List<String>) session.getAttribute("systemUserRigthList"));
		map.put("userInfoSession",
				(UserInfo) session.getAttribute("systemUserInfo"));
		map.put("languageId", Integer.valueOf(languageId));
		map.put("newsId", Integer.valueOf(newsId));
		map.put("relId", Integer.valueOf(relId));
		map.put("act", "update");
		map.putAll(this.environment);
		List<UserInfo> userInfoList = this.userInfoDao.getUserListByRoleId(-1, 0,
				2147483647);
		map.put("userInfoList", userInfoList);
		mav.addAllObjects(map);
		return mav;
	}

	@RequestMapping({ "/update" })
	public ModelAndView update(HttpServletRequest request, HttpSession session,
			ModelMap map) throws Exception {
		ModelAndView mav = new ModelAndView("admin/common/message");
		String message = "";
		UserInfo userInfoSession = (UserInfo) session
				.getAttribute("systemUserInfo");
		int userId = StringUtils.isNotBlank(request.getParameter("userId")) ? Integer
				.parseInt(request.getParameter("userId")) : 0;
		int newsId = StringUtils.isNotBlank(request.getParameter("newsId")) ? Integer
				.parseInt(request.getParameter("newsId")) : 0;
		int relId = StringUtils.isNotBlank(request.getParameter("relId")) ? Integer
				.parseInt(request.getParameter("relId")) : 0;
		int languageId = StringUtils.isNotBlank(request
				.getParameter("languageId")) ? Integer.parseInt(request
				.getParameter("languageId")) : 1;
		String title = request.getParameter("title");
		String tagWordsId = request.getParameter("tagWordsId");
		String channelIds = request.getParameter("channelIds");

		String from = request.getParameter("from");
		String publishTime = request.getParameter("publishTime");
		int isFouceImage = StringUtils.isNotBlank(request
				.getParameter("isFouceImage")) ? Integer.parseInt(request
				.getParameter("isFouceImage")) : 0;
		String keywords = request.getParameter("keywords");
		String newsMeta = request.getParameter("newsMeta");

		int isShowInIndex = StringUtils.isNotBlank(request
				.getParameter("isShowInIndex")) ? Integer.parseInt(request
				.getParameter("isShowInIndex")) : 0;
		int isShowInList = StringUtils.isNotBlank(request
				.getParameter("isShowInList")) ? Integer.parseInt(request
				.getParameter("isShowInList")) : 1;
		int isTop = StringUtils.isNotBlank(request.getParameter("isTop")) ? Integer
				.parseInt(request.getParameter("isTop")) : 0;
		int isReview = StringUtils.isNotBlank(request.getParameter("isReview")) ? Integer
				.parseInt(request.getParameter("isReview")) : 0;

		int textType = StringUtils.isNotBlank(request.getParameter("textType")) ? Integer
				.parseInt(request.getParameter("textType")) : 1;
		String content = StringUtils
				.isNotBlank(request.getParameter("content")) ? request
				.getParameter("content") : "";
		String simpleText = StringUtils.isNotBlank(request
				.getParameter("simpleText")) ? request
				.getParameter("simpleText") : "";

		if (textType == 2) {
			content = simpleText;
		}

		String mainImage = "";
		String srcRegex = "src\\s*=\\s*[\"|'](.+?[jpg|gif|png|bmp])[\"|']";
		List<String> allImages = RegexUtils.getStringGoup1(content, srcRegex);
		//图片倒序排列
		List<String> reverseOrder = new ArrayList<String>();
		for(int i = allImages.size() -1 ; i > -1 ; i --){
			reverseOrder.add(allImages.get(i));
		}
//		Collections.sort(allImages, Collections.reverseOrder());
		String appPath = request.getContextPath();
		String appURL = request.getScheme() + "://" + request.getServerName()
				+ ":" + request.getServerPort();
		String contentResult = content;
		for (String image : reverseOrder) {
			if (!image.toLowerCase().startsWith("http")) {
				if ((StringUtils.isNotBlank(appPath))
						&& (appPath.length() == 1)) {
					mainImage = image.substring(1);
					contentResult = content.replace(image, appURL + image);
				} else {
					mainImage = image.substring(appPath.length() + 1);
					contentResult = content.replace(image, appURL + image);
				}
			} else {
				mainImage = image;
			}
		}
		NewsDTO newsDTO = new NewsDTO();
		NewsInfo updateInfo = this.newsInfoDao.getNewsInfoByNewsId(newsId);
		updateInfo.setNewsId(newsId);
		updateInfo.setLanguageId(languageId);
		updateInfo.setTitle(title);
		updateInfo.setTag(tagWordsId);

		if (userId > 0) {
			UserInfo userInfoSelect = this.userInfoDao.getUser(userId);
			updateInfo.setAuthor(userInfoSelect.getRealName());
			updateInfo.setUserId(userId);
		}
		updateInfo.setFrom(from);
		updateInfo.setPublishTime(publishTime);
		updateInfo.setIsFouceImage(isFouceImage);
		updateInfo.setMainImage(mainImage);
		updateInfo.setKeywords(keywords);
		updateInfo.setNewsMeta(newsMeta);
		updateInfo.setTextType(textType);
		updateInfo.setContent(contentResult);

		updateInfo.setChannelIds(channelIds);

		NewsChannelRel rel = new NewsChannelRel();
		rel.setId(relId);
		rel.setIsShowInIndex(isShowInIndex);
		rel.setIsShowInList(isShowInList);
		rel.setIsReview(isReview);
		rel.setIsTop(isTop);
		newsDTO.setNewsInfo(updateInfo);
		newsDTO.setRel(rel);

		if (this.newsInfoDao.update(newsDTO)) {
			message = "文章更新成功!";
			for (String cId : channelIds.split(","))
				this.logInfoDao.log(userInfoSession.getId(), 3, Integer
						.valueOf(cId).intValue(), "修改:" + title);
		} else {
			message = "文章更新失败!";
		}
		map.put("message", message);
		mav.addAllObjects(map);
		return mav;
	}

	@SuppressWarnings("unchecked")
	@RequestMapping({ "/list" })
	public ModelAndView list(HttpServletRequest request, HttpSession session,
			ModelMap map) throws Exception {
		ModelAndView mav = new ModelAndView("admin/newsInfo/list/news");
		UserInfo userInfoSession = (UserInfo) session
				.getAttribute("systemUserInfo");
		List<String> userRightsSession = (List<String>) session
				.getAttribute("systemUserRigthList");

		int languageId = StringUtils.isNotBlank(request
				.getParameter("languageId")) ? Integer.parseInt(request
				.getParameter("languageId")) : 1;
		int roleId = userInfoSession.getRoleId();
		int userId = StringUtils.isNotBlank(request.getParameter("userId")) ? Integer
				.parseInt(request.getParameter("userId")) : userInfoSession
				.getId();
		if ((roleId == 1) && (userId == userInfoSession.getId())) {
			userId = 0;
		}
		String keywords = StringUtils.isNotBlank(request
				.getParameter("keywords")) ? request.getParameter("keywords")
				: "";
		keywords = StringTools.converISO2UTF8(keywords);
		int isFouceImage = StringUtils.isNotBlank(request
				.getParameter("isFouceImage")) ? Integer.parseInt(request
				.getParameter("isFouceImage")) : -1;
		int channelId = StringUtils.isNotBlank(request
				.getParameter("channelId")) ? Integer.parseInt(request
				.getParameter("channelId")) : 1;
		int isTop = StringUtils.isNotBlank(request.getParameter("isTop")) ? Integer
				.parseInt(request.getParameter("isTop")) : -1;
		int isShowInIndex = StringUtils.isNotBlank(request
				.getParameter("isShowInIndex")) ? Integer.parseInt(request
				.getParameter("isShowInIndex")) : -1;
		int isShowInList = StringUtils.isNotBlank(request
				.getParameter("isShowInList")) ? Integer.parseInt(request
				.getParameter("isShowInList")) : -1;
		int isReview = StringUtils.isNotBlank(request.getParameter("isReview")) ? Integer
				.parseInt(request.getParameter("isReview")) : -1;

		int curPage = StringUtils.isNotBlank(request.getParameter("curPage")) ? Integer
				.parseInt(request.getParameter("curPage")) : 1;
		PageBean pageBean = new PageBean();
		pageBean.setCurPage(curPage);
		QueryNewsObject query = new QueryNewsObject();
		query.setLanguageId(languageId);
		query.setKeywords(keywords);
		query.setIsFouceImage(isFouceImage);
		query.setIsShowInIndex(isShowInIndex);
		query.setIsShowInList(isShowInList);
		query.setIsTop(isTop);
		query.setIsReview(isReview);
		query.setStart(pageBean.getStart());
		query.setNum(pageBean.getRowsPerPage());
		query.setOrderBy(1);
		query.setUserId(userId);

		List<NewsDTO> newsDTOList = this.newsInfoDao.getNewsListByQueryPChannelId(query,
				channelId);
		pageBean.setMaxRowCount(this.newsInfoDao.getNewsCountByQueryPChannelId(
				query, channelId));
		pageBean.setMaxPage();
		pageBean.setPageNoList();

		List<LanguageInfo> languageInfoList = this.languageInfoDao.getAll();

		Map<Integer,String> channelMap = this.channelInfoDao.getAllChannelAsMap();
		Map<Integer,String> languageMap = this.languageInfoDao.getLanuageMap();
		List<UserInfo> userInfoList = new UserInfoDao().getUserListByRoleId(0, 0,
				2147483647);

		map.put("channelMap", channelMap);
		map.put("languageMap", languageMap);
		map.put("userInfoList", userInfoList);
		map.put("pageBean", pageBean);
		map.put("newsDTOList", newsDTOList);
		map.put("languageInfoList", languageInfoList);
		map.put("userInfoSession", userInfoSession);
		map.put("userRightsSession", userRightsSession);
		map.put("channelId", Integer.valueOf(channelId));
		map.put("query", query);
		map.putAll(this.environment);
		mav.addAllObjects(map);
		return mav;
	}

	@RequestMapping({ "/setDestbin" })
	public ModelAndView setDestbin(HttpServletRequest request, ModelMap map)
			throws Exception {
		ModelAndView mav = new ModelAndView("admin/common/message");
		String message = "";
		String relIds = request.getParameter("relIds");
		int flag = StringUtils.isNotBlank(request.getParameter("flag")) ? Integer
				.valueOf(request.getParameter("flag")).intValue() : 0;
		String flagString = "成功!";
		for (String relId : relIds.split(",")) {
			if (!this.newsInfoDao.setDestbin(Integer.valueOf(relId).intValue(),
					flag)) {
				flagString = "失败!";
				break;
			}
		}
		message = "从回收站中还原" + flagString;

		map.put("message", message);
		mav.addAllObjects(map);
		return mav;
	}

	@RequestMapping({ "/deleteNews" })
	public ModelAndView deleteNews(HttpServletRequest request, ModelMap map)
			throws Exception {
		ModelAndView mav = new ModelAndView("admin/common/message");
		String message = "";
		String relIds = request.getParameter("relIds");
		if (StringUtils.isBlank(relIds)) {
			map.put("message", "无效的操作!");
			mav.addAllObjects(map);
			return mav;
		}
		message = "从回收站中删除成功 ";
		for (String relId : relIds.split(",")) {
			NewsChannelRel newsRel = this.newsInfoDao.getNewsRelById(Integer
					.valueOf(relId).intValue());
			if (!this.newsInfoDao.deleteNews(newsRel.getNewsId(), Integer
					.valueOf(relId).intValue())) {
				message = "从回收站中删除失败!";
				break;
			}
		}
		map.put("message", message);
		mav.addAllObjects(map);
		return mav;
	}

	@RequestMapping({ "/setNewsTop" })
	public ModelAndView setNewsTop(HttpServletRequest request, ModelMap map)
			throws Exception {
		ModelAndView mav = new ModelAndView("admin/common/message");
		String message = "";
		String relIds = request.getParameter("relIds");
		int flag = StringUtils.isNotBlank(request.getParameter("flag")) ? Integer
				.valueOf(request.getParameter("flag")).intValue() : 0;
		String flagString = "成功!";
		for (String relId : relIds.split(",")) {
			if (!this.newsInfoDao.setNewsTopOrNot(Integer.valueOf(relId)
					.intValue(), flag)) {
				flagString = "失败!";
				break;
			}
		}
		message = "取消置顶" + flagString;
		map.put("message", message);
		mav.addAllObjects(map);
		return mav;
	}

	@RequestMapping({ "/setPageShow" })
	public ModelAndView setPageShow(HttpServletRequest request, ModelMap map)
			throws Exception {
		ModelAndView mav = new ModelAndView("admin/common/message");
		String message = "";
		String relIds = request.getParameter("relIds");
		int flag = StringUtils.isNotBlank(request.getParameter("flag")) ? Integer
				.valueOf(request.getParameter("flag")).intValue() : 0;
		int pageIndex = StringUtils.isNotBlank(request
				.getParameter("pageIndex")) ? Integer.valueOf(
				request.getParameter("pageIndex")).intValue() : 1;
		String pageShowString = pageIndex == 1 ? " 首页  " : " 列表页 ";
		String flagString = "成功!";
		for (String relId : relIds.split(",")) {
			if (!this.newsInfoDao.setPageShow(
					Integer.valueOf(relId).intValue(), flag, pageIndex)) {
				flagString = "失败!";
				break;
			}
		}
		message = "取消" + pageShowString + "显示" + flagString;
		map.put("message", message);
		mav.addAllObjects(map);
		return mav;
	}

	@RequestMapping({ "/setReview" })
	public ModelAndView setReview(HttpServletRequest request, ModelMap map)
			throws Exception {
		ModelAndView mav = new ModelAndView("admin/common/message");
		String message = "";
		String relIds = request.getParameter("relIds");
		int flag = StringUtils.isNotBlank(request.getParameter("flag")) ? Integer
				.valueOf(request.getParameter("flag")).intValue() : 0;
		String flagString = "成功!";
		for (String relId : relIds.split(",")) {
			if (!this.newsInfoDao.setReview(Integer.valueOf(relId).intValue(),
					flag)) {
				flagString = "失败!";
				break;
			}
		}
		message = "取消审核" + flagString;
		map.put("message", message);
		mav.addAllObjects(map);
		return mav;
	}

	@RequestMapping({ "/sortNews" })
	public ModelAndView sortNews(HttpServletRequest request,
			HttpServletResponse response, ModelMap map) throws Exception {
		ModelAndView mav = new ModelAndView("admin/common/message");
		String message = "";
		String referer = request.getHeader("Referer");
		int newsId = StringUtils.isNotBlank(request.getParameter("newsId")) ? Integer
				.valueOf(request.getParameter("newsId")).intValue() : 0;
		int orderId = StringUtils.isNotBlank(request.getParameter("orderId")) ? Integer
				.valueOf(request.getParameter("orderId")).intValue() : 0;
		String flag = request.getParameter("flag");
		int newsChannelId = StringUtils.isNotBlank(request
				.getParameter("newsChannelId")) ? Integer.valueOf(
				request.getParameter("newsChannelId")).intValue() : 1;
		if (this.newsInfoDao.sortNews(newsId, orderId, flag, newsChannelId)) {
			message = "新闻排序成功!";
			response.sendRedirect(referer);
		}
		map.put("message", message);
		mav.addAllObjects(map);
		return mav;
	}

	@RequestMapping({ "/noRight" })
	public ModelAndView noRight(ModelMap map) throws Exception {
		ModelAndView mav = new ModelAndView("admin/common/message");
		String message = "暂无权限操作，请联系管理员分配权限 ";
		map.put("message", message);
		mav.addAllObjects(map);
		return mav;
	}

	@SuppressWarnings("unchecked")
	@RequestMapping({ "/listdestbin" })
	public ModelAndView listdestbin(HttpServletRequest request,
			HttpSession session, ModelMap map) throws Exception {
		ModelAndView mav = new ModelAndView("admin/newsInfo/list/listDestbin");
		UserInfo userInfoSession = (UserInfo) session
				.getAttribute("systemUserInfo");
		List<String> userRightsSession = (List<String>) session
				.getAttribute("systemUserRigthList");

		int languageId = StringUtils.isNotBlank(request
				.getParameter("languageId")) ? Integer.parseInt(request
				.getParameter("languageId")) : 1;
		int roleId = userInfoSession.getRoleId();
		int userId = StringUtils.isNotBlank(request.getParameter("userId")) ? Integer
				.parseInt(request.getParameter("userId")) : userInfoSession
				.getId();
		if ((roleId == 1) && (userId == userInfoSession.getId())) {
			userId = 0;
		}
		String keywords = StringUtils.isNotBlank(request
				.getParameter("keywords")) ? request.getParameter("keywords")
				: "";
		keywords = StringTools.converISO2UTF8(keywords);
		int channelId = StringUtils.isNotBlank(request
				.getParameter("channelId")) ? Integer.parseInt(request
				.getParameter("channelId")) : 0;
		int curPage = StringUtils.isNotBlank(request.getParameter("curPage")) ? Integer
				.parseInt(request.getParameter("curPage")) : 1;
		PageBean pageBean = new PageBean();
		pageBean.setCurPage(curPage);
		QueryNewsObject query = new QueryNewsObject();
		query.setLanguageId(languageId);
		query.setKeywords(keywords);
		query.setIsDelete(1);
		query.setStart(pageBean.getStart());
		query.setNum(pageBean.getRowsPerPage());

		List<NewsDTO> newsDTOList = this.newsInfoDao.getNewsListByQueryPChannelId(query,
				channelId);
		pageBean.setMaxRowCount(this.newsInfoDao.getNewsCountByQueryPChannelId(
				query, channelId));
		pageBean.setMaxPage();
		pageBean.setPageNoList();

		Map<Integer,String> channelMap = this.channelInfoDao.getAllChannelAsMap();
		Map<Integer,String> languageMap = this.languageInfoDao.getLanuageMap();
		List<ChannelInfo> channelInfoList = this.channelInfoDao
				.getSubListByParentIdLanguageId(languageId, 0, 0, 2147483647);
		List<UserInfo> userInfoList = new UserInfoDao().getUserListByRoleId(0, 0,
				2147483647);
		List<LanguageInfo> languageInfoList = this.languageInfoDao.getAll();

		map.put("channelMap", channelMap);
		map.put("languageMap", languageMap);
		map.put("userInfoList", userInfoList);
		map.put("channelInfoList", channelInfoList);
		map.put("languageInfoList", languageInfoList);

		map.put("pageBean", pageBean);
		map.put("newsDTOList", newsDTOList);
		map.put("userInfoSession", userInfoSession);
		map.put("userRightsSession", userRightsSession);
		map.put("channelId", Integer.valueOf(channelId));
		map.put("query", query);
		map.putAll(this.environment);
		mav.addAllObjects(map);
		return mav;
	}

	@RequestMapping(value = { "/showNewsSynch" }, method = { org.springframework.web.bind.annotation.RequestMethod.GET })
	public ModelAndView showNewsSynch(HttpServletRequest request,
			HttpServletResponse response, HttpSession session, ModelMap map)
			throws Exception {
		ModelAndView mav = new ModelAndView(
				"admin/contentInfo/showContentSynch");
		String newsIds = WebUtils
				.getRequestParameterAsString(request, "newsId");
		if (StringUtils.isBlank(newsIds)) {
			map.put("message", "请选择要同步的新闻!");
			mav.addAllObjects(map);
			mav.setViewName("admin/common/message");
			return mav;
		}
		int websiteSelfId = 0;
		List<WebsiteInfo> websiteInfoList = this.websiteInfoDao.getSubList(null, 0,
				2147483647);
		for (WebsiteInfo website : websiteInfoList) {
			if (website.getIsSelf() == 1) {
				websiteSelfId = website.getId();
			}
		}

		List<ContentInfo> contentInfoList = new ArrayList<ContentInfo>();
		String[] ids = newsIds.split(",");
		NewsInfo newsInfo = null;
		ContentInfo contentInfo = new ContentInfo();
		for (String newsId : ids) {
			newsInfo = this.newsInfoDao.getNewsInfoByNewsId(Integer.valueOf(
					newsId).intValue());
			contentInfo.setContentId(StringTools.getUUID());
			contentInfo.setTitle(newsInfo.getTitle());
			contentInfo.setWebsiteId(websiteSelfId);
			contentInfo.setText(newsInfo.getContent());
			contentInfo.setUserId(((UserInfo) session
					.getAttribute("systemUserInfo")).getId());
			contentInfo.setPublishTime(DateFormatUtils.format(new Date(),
					"yyyy-MM-dd HH:mm:ss"));
			contentInfoList.add(contentInfo);

			this.contentInfoDao.insert(contentInfo);
		}
		map.put("websiteInfoList", websiteInfoList);
		map.put("contentInfoList", contentInfoList);
		mav.addAllObjects(map);
		return mav;
	}
}